<template>
  <transition name="el-message-fade">
    <div class="notice-wrapper"
      v-show="visible">
      <div class="notice-content">
        <div class="notice-header">
          <aside>异常提醒</aside>
          <aside @click="handleClose">
            <i class="fa fa-times"
              aria-hidden="true"></i>
            <span>{{content.len}}</span>
          </aside>
        </div>
        <div class="notice-body">
          <p>发生时间：<span>{{content.time}}</span></p>
          <p>发生企业：<span>{{content.companyName}}</span></p>
          <p>发生设备：<span>{{content.deviceName}}</span></p>
          <p>发生位置：<span>{{content.location}}</span></p>
          <p>事件详情：<span>{{content.content}}</span></p>
          <h3>请尽快处理！</h3>
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
import _ from 'lodash'
export default {
  name: 'Notice',
  data() {
    return {
      visible: false,
      content: {},
      duration: 3000
    }
  },
  methods: {
    setTimer() {
      // setTimeout(() => {
      //   this.close()
      // }, this.duration)
    },

    close() {
      this.visible = false
      setTimeout(() => {
        this.$destroy(true)
        try {
          this.$nextTick(() => {
            this.$el.parentNode.removeChild(this.$el)
            this.content.callback(this)
          })
        } catch (error) {
          console.log(error)
        }
      }, 300)
    },
    // 这里注意要是用普通函数，箭头函数会导致this丢失
    handleClose: _.debounce(function() {
      this.close()
    }, 500)
  },
  mounted() {
    // console.log(this.content)
    this.setTimer()
  }
}
</script>
<style lang="scss" scoped>
.notice {
  &-wrapper {
    position: fixed;
    z-index: 999;
    right: 0;
    bottom: 0;
  }
  &-content {
    width: 300px;
    min-height: 200px;
    position: relative;
    display: flex;
    flex-direction: column;
  }
  &-header {
    height: 20%;
    width: 100%;
    box-sizing: border-box;
    padding: 0 10px;
    background-color: #199ed8;
    color: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    aside:nth-child(2) {
      height: 100%;
      width: 50px;
      text-align: right;
      line-height: 2.5;
      cursor: pointer;
      user-select: none;
    }
  }
  &-body {
    // height: 80%;
    width: 100%;
    padding: 8px;
    box-sizing: border-box;
    background-color: #fff;
    color: #222;
    line-height: 1.5;
    overflow: hidden;
    > p {
      font-size: 14px;
    }
    > h3 {
      margin-top: 5px;
      font-size: 18px;
    }
  }
}
</style>

